// 插件
let Vue
class Store {
    constructor(options) {
      this._mutations = options.mutations
      this._actions = options.actions

      // 响应式的数据State
      // this.state = new Vue({
      //   data:options.state
      // })
      this._vm = new Vue({
        data:{
          // 目的：vue不会对有下划线或$$开头的key进行代理，所以就不会对外暴露
          $$state:options.state
        }
      })
      this.commit = this.commit.bind(this)
      this.dispatch = this.dispatch.bind(this)
    }
    get state() {
      // console.log(this._vm,'============');
      return this._vm.$data.$$state
    }
    set state(v) {
      console.error('违规操作');
    }
    // 修改状态 commint('add',,payload)
    commit(type,payload){
      let mutations = this._mutations[type]
      if (!mutations) {
        console.error('mutations不存在');
        return
      }
      mutations(this.state,payload)
    }
    dispatch(type,payload){
      let action = this._actions[type]
      if (!action) {
        console.error('action不存在');
        return
      }
      action(this,payload)
    }
}
function install(_Vue) {
  Vue = _Vue

  // 先注册$store
  Vue.mixin({
    beforeCreate(){
      if(this.$options.store){
        Vue.prototype.$store =this.$options.store 
      }
    }
  })
}

export default { Store,install}